﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace EcoSystem
{
    public class DataPermissionAppService : CrudAppService<
        DataPermission,
        DataPermissionDto,
        Guid,
        GetDataPermissionInput,
        CreateOrUpdateDataPermissionDto,
        CreateOrUpdateDataPermissionDto>, IDataPermissionAppService
    {
        public DataPermissionAppService(IRepository<DataPermission, Guid> repository) : base(repository)
        {

        }

        public override async Task<PagedResultDto<DataPermissionDto>> GetListAsync(GetDataPermissionInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                .WhereIf(!input.EnCode.IsNullOrWhiteSpace(), x => x.EnCode.Equals(input.EnCode))
                .WhereIf(!input.Name.IsNullOrWhiteSpace(), x => x.Name.Contains(input.Name));
            var total = await query.CountAsync();
            var items = await query.OrderBy(x => x.Id).PageBy(input).ToListAsync();
            return new PagedResultDto<DataPermissionDto>()
            {
                Items = await MapToGetListOutputDtosAsync(items),
                TotalCount = total
            };
        }

        public async Task<List<DataPermissionDto>> GetAllListAsync(GetDataPermissionInput input)
        {
            var query = (await Repository.GetQueryableAsync()).AsNoTracking()
                .WhereIf(!input.EnCode.IsNullOrWhiteSpace(), x => x.EnCode.Equals(input.EnCode))
                .WhereIf(!input.Name.IsNullOrWhiteSpace(), x => x.Name.Contains(input.Name));
            var items = await query.OrderBy(x => x.Id).ToListAsync();
            return await MapToGetListOutputDtosAsync(items);
        }
    }

}
